{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "fw = open(\"MapDataOutput1.kml\",\"w+\")\n",
    "f = open(\"LatLong.txt\",\"r\")\n",
    "test = open(\"CONFIGURE/Basis.kml\",\"r\")  #open(\"CONFIGURE/t.kml\",\"r\")#,encoding='gb18030',errors='ignore')#,encoding='UTF-8')#,encoding='gb18030',errors='ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [],
   "source": [
    "for line in test:\n",
    "    state = line.find(\"<Placemark>\")\n",
    "    if(state == -1):\n",
    "        fw.write(line)\n",
    "    else:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:a1 lat:120.520163178378 lon:36.89206629053305\n",
      "name:a2 lat:120.5193894447255 lon:36.89284333308383\n",
      "name:a3 lat:120.5190596853301 lon:36.89239965021736\n",
      "name:a4 lat:120.5162256 lon:36.8934955\n"
     ]
    }
   ],
   "source": [
    "f.seek(0,0)\n",
    "for line in f:\n",
    "    spliter = line.find(\":\")\n",
    "    name = line[:spliter]\n",
    "    spliter_latlon = line.find(\",\")\n",
    "    lon = line[spliter+1:spliter_latlon]\n",
    "    line =line[spliter_latlon+1:]\n",
    "    spliter_latlon = line.find(\",\")\n",
    "    lat = line[:spliter_latlon]\n",
    "    print( \"\\t\\t\\t<Placemark>\\n\\t\\t\\t<name>{0}</name>\\n\\t\\t\\t<LookAt>\\n\\t\\t\\t\\t<longitude>{1}</longitude>\\n\\t\\t\\t\\t<latitude>{2}</latitude>\\n\\t\\t\\t\\t<altitude>0</altitude>\\n\\t\\t\\t\\t<heading>0</heading>\\n\\t\\t\\t\\t<tilt>0</tilt>\\n\\t\\t\\t\\t<range>728.8612258819933</range>\\n\\t\\t\\t\\t<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>\\n\\t\\t\\t\\t</LookAt>\\n\\t\\t\\t\\t<styleUrl>#m_ylw-pushpin</styleUrl>\\n\\t\\t\\t<Point>\\n\\t\\t\\t\\t<gx:drawOrder>1</gx:drawOrder>\\n\\t\\t\\t\\t<coordinates>{1},{2},0</coordinates>\\n\\t\\t\\t\\t</Point>\\n\\t\\t\\t</Placemark>\\n\".format(name,lon,lat),file=fw)\n",
    "    print(\"name:%s lat:%s lon:%s\"%(name,lon,lat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"</Folder>\\n</Document>\\n</kml>\\n\",file=fw)\n",
    "fw.close()\n",
    "f.close()\n",
    "test.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
      "\n",
      "<kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n",
      "\n",
      "<Document>\n",
      "\n",
      "\t<name>MapData.kml</name>\n",
      "\n",
      "\t<Style id=\"s_ylw-pushpin_hl\">\n",
      "\n",
      "\t\t<IconStyle>\n",
      "\n",
      "\t\t\t<scale>1.3</scale>\n",
      "\n",
      "\t\t\t<Icon>\n",
      "\n",
      "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\n",
      "\n",
      "\t\t\t</Icon>\n",
      "\n",
      "\t\t\t<hotSpot x=\"20\" y=\"2\" xunits=\"pixels\" yunits=\"pixels\"/>\n",
      "\n",
      "\t\t</IconStyle>\n",
      "\n",
      "\t</Style>\n",
      "\n",
      "\t<StyleMap id=\"m_ylw-pushpin\">\n",
      "\n",
      "\t\t<Pair>\n",
      "\n",
      "\t\t\t<key>normal</key>\n",
      "\n",
      "\t\t\t<styleUrl>#s_ylw-pushpin</styleUrl>\n",
      "\n",
      "\t\t</Pair>\n",
      "\n",
      "\t\t<Pair>\n",
      "\n",
      "\t\t\t<key>highlight</key>\n",
      "\n",
      "\t\t\t<styleUrl>#s_ylw-pushpin_hl</styleUrl>\n",
      "\n",
      "\t\t</Pair>\n",
      "\n",
      "\t</StyleMap>\n",
      "\n",
      "\t<Style id=\"s_ylw-pushpin\">\n",
      "\n",
      "\t\t<IconStyle>\n",
      "\n",
      "\t\t\t<scale>1.1</scale>\n",
      "\n",
      "\t\t\t<Icon>\n",
      "\n",
      "\t\t\t\t<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\n",
      "\n",
      "\t\t\t</Icon>\n",
      "\n",
      "\t\t\t<hotSpot x=\"20\" y=\"2\" xunits=\"pixels\" yunits=\"pixels\"/>\n",
      "\n",
      "\t\t</IconStyle>\n",
      "\n",
      "\t</Style>\n",
      "\n",
      "\t<Folder>\n",
      "\n",
      "\t\t<name>涓存椂浣嶇疆</name>\n",
      "\n",
      "\t\t<open>1</open>\n",
      "\n",
      "\t\t\t<Placemark>\n",
      "\n",
      "\t\t\t<name>a1</name>\n",
      "\n",
      "\t\t\t<LookAt>\n",
      "\n",
      "\t\t\t\t<longitude>120.520163178378</longitude>\n",
      "\n",
      "\t\t\t\t<latitude>36.89206629053305</latitude>\n",
      "\n",
      "\t\t\t\t<altitude>0</altitude>\n",
      "\n",
      "\t\t\t\t<heading>0</heading>\n",
      "\n",
      "\t\t\t\t<tilt>0</tilt>\n",
      "\n",
      "\t\t\t\t<range>728.8612258819933</range>\n",
      "\n",
      "\t\t\t\t<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>\n",
      "\n",
      "\t\t\t\t</LookAt>\n",
      "\n",
      "\t\t\t\t<styleUrl>#m_ylw-pushpin</styleUrl>\n",
      "\n",
      "\t\t\t<Point>\n",
      "\n",
      "\t\t\t\t<gx:drawOrder>1</gx:drawOrder>\n",
      "\n",
      "\t\t\t\t<coordinates>120.520163178378,36.89206629053305,0</coordinates>\n",
      "\n",
      "\t\t\t\t</Point>\n",
      "\n",
      "\t\t\t</Placemark>\n",
      "\n",
      "\n",
      "\n",
      "\t\t\t<Placemark>\n",
      "\n",
      "\t\t\t<name>a2</name>\n",
      "\n",
      "\t\t\t<LookAt>\n",
      "\n",
      "\t\t\t\t<longitude>120.5193894447255</longitude>\n",
      "\n",
      "\t\t\t\t<latitude>36.89284333308383</latitude>\n",
      "\n",
      "\t\t\t\t<altitude>0</altitude>\n",
      "\n",
      "\t\t\t\t<heading>0</heading>\n",
      "\n",
      "\t\t\t\t<tilt>0</tilt>\n",
      "\n",
      "\t\t\t\t<range>728.8612258819933</range>\n",
      "\n",
      "\t\t\t\t<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>\n",
      "\n",
      "\t\t\t\t</LookAt>\n",
      "\n",
      "\t\t\t\t<styleUrl>#m_ylw-pushpin</styleUrl>\n",
      "\n",
      "\t\t\t<Point>\n",
      "\n",
      "\t\t\t\t<gx:drawOrder>1</gx:drawOrder>\n",
      "\n",
      "\t\t\t\t<coordinates>120.5193894447255,36.89284333308383,0</coordinates>\n",
      "\n",
      "\t\t\t\t</Point>\n",
      "\n",
      "\t\t\t</Placemark>\n",
      "\n",
      "\n",
      "\n",
      "\t\t\t<Placemark>\n",
      "\n",
      "\t\t\t<name>a3</name>\n",
      "\n",
      "\t\t\t<LookAt>\n",
      "\n",
      "\t\t\t\t<longitude>120.5190596853301</longitude>\n",
      "\n",
      "\t\t\t\t<latitude>36.89239965021736</latitude>\n",
      "\n",
      "\t\t\t\t<altitude>0</altitude>\n",
      "\n",
      "\t\t\t\t<heading>0</heading>\n",
      "\n",
      "\t\t\t\t<tilt>0</tilt>\n",
      "\n",
      "\t\t\t\t<range>728.8612258819933</range>\n",
      "\n",
      "\t\t\t\t<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>\n",
      "\n",
      "\t\t\t\t</LookAt>\n",
      "\n",
      "\t\t\t\t<styleUrl>#m_ylw-pushpin</styleUrl>\n",
      "\n",
      "\t\t\t<Point>\n",
      "\n",
      "\t\t\t\t<gx:drawOrder>1</gx:drawOrder>\n",
      "\n",
      "\t\t\t\t<coordinates>120.5190596853301,36.89239965021736,0</coordinates>\n",
      "\n",
      "\t\t\t\t</Point>\n",
      "\n",
      "\t\t\t</Placemark>\n",
      "\n",
      "\n",
      "\n",
      "\t\t\t<Placemark>\n",
      "\n",
      "\t\t\t<name>a4</name>\n",
      "\n",
      "\t\t\t<LookAt>\n",
      "\n",
      "\t\t\t\t<longitude>120.5162256</longitude>\n",
      "\n",
      "\t\t\t\t<latitude>36.8934955</latitude>\n",
      "\n",
      "\t\t\t\t<altitude>0</altitude>\n",
      "\n",
      "\t\t\t\t<heading>0</heading>\n",
      "\n",
      "\t\t\t\t<tilt>0</tilt>\n",
      "\n",
      "\t\t\t\t<range>728.8612258819933</range>\n",
      "\n",
      "\t\t\t\t<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>\n",
      "\n",
      "\t\t\t\t</LookAt>\n",
      "\n",
      "\t\t\t\t<styleUrl>#m_ylw-pushpin</styleUrl>\n",
      "\n",
      "\t\t\t<Point>\n",
      "\n",
      "\t\t\t\t<gx:drawOrder>1</gx:drawOrder>\n",
      "\n",
      "\t\t\t\t<coordinates>120.5162256,36.8934955,0</coordinates>\n",
      "\n",
      "\t\t\t\t</Point>\n",
      "\n",
      "\t\t\t</Placemark>\n",
      "\n",
      "\n",
      "\n",
      "</Folder>\n",
      "\n",
      "</Document>\n",
      "\n",
      "</kml>\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "fw = open(\"MapDataOutput1.kml\",\"r\")\n",
    "for line in fw:\n",
    "    print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [],
   "source": [
    "fw.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
